﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using SimpleServiceBus.Utilities.ObjectBuilder;

namespace SimpleServiceBus.EndpointManagement.Server.Wcf
{
    public class InstanceProvider : IInstanceProvider
    {
        private readonly Type _serviceType;
        private readonly IObjectBuilder _ioc;

        public InstanceProvider(Type serviceType) : this(serviceType, null)
        {
        }

        public InstanceProvider(Type serviceType, IObjectBuilder ioc)
        {
            _serviceType = serviceType;
            _ioc = ioc ?? new DefaultObjectBuilder();
        }

        public object GetInstance(InstanceContext instanceContext)
        {
            return GetInstance(instanceContext, null);
        }

        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return _ioc.Build(_serviceType);
        }

        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
        }
    }
}
